//
//  YZScrollView.m
//  kuaiyi
//
//  Created by WGP on 16/5/6.
//
//

#import "YZScrollView.h"
#import "YZFormView.h"
#import <Masonry.h>
#define ScrollWidth self.bounds.size.width
#define ScrollHeight self.bounds.size.height

@interface YZScrollView ()<YZFormViewDelegate>

@property(nonatomic,strong)UITextView *conditionLabel;
@property(nonatomic,strong)UITextView *briefLabel;
@property(nonatomic,strong)YZFormView *formView;

@end
@implementation YZScrollView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    self.pagingEnabled = YES;
    
    self.conditionLabel = [[UITextView alloc] init];
    self.conditionLabel.editable = NO;
    self.conditionLabel.font = [UIFont systemFontOfSize:18];
    [self addSubview:self.conditionLabel];

    self.briefLabel = [[UITextView alloc] init];
    self.briefLabel.editable = NO;
    self.briefLabel.font = [UIFont systemFontOfSize:18];
    [self addSubview:self.briefLabel];
    
    
    self.formView = [YZFormView new];
    self.formView.delegate = self;
    self.formView.hasNumberArr = @[[NSIndexPath indexPathForRow:1 inSection:1],
                              [NSIndexPath indexPathForRow:5 inSection:2],
                              [NSIndexPath indexPathForRow:3 inSection:3]];
    [self addSubview:self.formView];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.conditionLabel.frame = CGRectMake(0, 0, ScrollWidth, ScrollHeight);
    self.briefLabel.frame = CGRectMake(ScrollWidth, 0, ScrollWidth, ScrollHeight);
    CGFloat height = ([UIScreen mainScreen].bounds.size.width/8+8)*4;
    self.formView.frame = CGRectMake(ScrollWidth*2, 40, ScrollWidth, height);
}

-(void)setDoctorModel:(YZDoctorModel *)doctorModel
{
    _doctorModel = doctorModel;
    [self setConditionLabelText];
    [self setBriefViewContent];
}

-(void)setConditionLabelText
{
    NSString *URL = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    NSDictionary *parameters = @{@"doctor_id":@300000315};
    [[YZNetWorkTool sharedNetWorkTool] POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSDictionary *data = dict[@"data"];
        NSString *receiving_setting_extra = data[@"receiving_setting_extra"];
        NSArray *receiving_settingsArr = data[@"receiving_settings"];

        NSMutableString *receiving_settings = [NSMutableString string];
        for (NSString *str in receiving_settingsArr) {
            NSString *string = [NSString stringWithFormat:@"%@ ",str];
            [receiving_settings appendString:string];
        }
        
        NSString *text = [NSString stringWithFormat:@"%@\n\n%@",receiving_setting_extra,receiving_settings];
        NSRange range = [text rangeOfString:receiving_setting_extra];
        NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithString:text];
        [attM addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
        [attM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, text.length)];
        self.conditionLabel.attributedText = attM;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

-(void)setBriefViewContent
{
    NSString *URL = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *parameters = @{@"doctor_id":@300000315};
    [[YZNetWorkTool sharedNetWorkTool] POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSDictionary *data = dict[@"data"];
        NSString *introduction = data[@"introduction"];
        self.briefLabel.text = introduction;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

-(void)formDidClick:(YZFormView *)formView hasNumber:(BOOL)hasNumber info:(NSDictionary *)infoDict
{
//    跳转界面
    if (hasNumber) {
        self.pushBlock();
    }
    NSLog(@"%@",self.doctorModel);
}

@end
